package com.example.bootproducer.conf;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    /**
     * 声明交换机
     */
    @Bean
    public Exchange topicEx(){
        return ExchangeBuilder
                .topicExchange("boot.topic.ex")
                .durable(true)
                .build();
    }

    /**
     * 声明队列1
     */
    @Bean
    public Queue topicQue1(){
        return QueueBuilder
                .durable("boot.topicQue1")
                .build();
    }
    /**
     * 声明队列2
     */
    @Bean
    public Queue topicQue2(){
        return QueueBuilder
                .durable("boot.topicQue2")
                .build();
    }

    /**
     * 绑定交换机和队列的关系
     */
    @Bean
    public Binding topicQue1Binding(Queue topicQue1,Exchange topicEx){
        return BindingBuilder
                .bind(topicQue1)
                .to(topicEx)
                .with("boot.msg.*")
                .noargs();
    }
    @Bean
    public Binding topicQue2Binding(Queue topicQue2,Exchange topicEx){
        return BindingBuilder
                .bind(topicQue2)
                .to(topicEx)
                .with("boot.#")
                .noargs();
    }

    /*=======================演示死信=============================*/
    /**
     * 普通交换机
     */
    @Bean
    public Exchange normalEx(){
        return ExchangeBuilder
                .topicExchange("normal.topic.ex")
                .durable(true)
                .build();
    }

    /**
     * 普通队列-- 当消息不被正确处理时，会成为死信，会将这条消息转发到死信交换机
     * @return
     */
    @Bean
    public Queue normalQue1(){
        return QueueBuilder
                .durable("normal.topicQue1")
                //指定死信交换机的名称
                .deadLetterExchange("deadletter.topic.ex")
                //指定转发到死信交换机的消的路由键
                .deadLetterRoutingKey("deadletter.abc")
                //指定当前队列最多存储5条消息
//                .maxLength(5)
                //设置消息超时时间 - 10s超时
//                .ttl(10000)
                .build();
    }
    /**
     * 绑定普通队列和普通交换机
     */
    @Bean
    public Binding normalQue1Binding(Queue normalQue1,Exchange normalEx){
        return BindingBuilder
                .bind(normalQue1)
                .to(normalEx)
                .with("normal.#")
                .noargs();
    }

    /**
     * 死信交换机
     */
    @Bean
    public Exchange deadletterEx(){
        return ExchangeBuilder
                .topicExchange("deadletter.topic.ex")
                .durable(true)
                .build();
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadletterQue1(){
        return QueueBuilder
                .durable("deadletter.topicQue1")
                .build();
    }
    /**
     * 绑定死信队列和死信交换机
     */
    @Bean
    public Binding deadletterQue1Binding(Queue deadletterQue1,Exchange deadletterEx){
        return BindingBuilder
                .bind(deadletterQue1)
                .to(deadletterEx)
                .with("deadletter.#")
                .noargs();
    }


    /*==================惰性队列================*/
    @Bean
    public Queue lazyQue1(){
        return QueueBuilder
                .durable("lazy.topicQue1")
                .lazy()
                .build();
    }
    @Bean
    public Binding lazyQue1Binding(Queue lazyQue1,Exchange normalEx){
        return BindingBuilder
                .bind(lazyQue1)
                .to(normalEx)
                .with("lazy.#")
                .noargs();
    }

    /*==================幂等性演示================*/
    @Bean
    public Queue midengQue1(){
        return QueueBuilder
                .durable("mideng.topicQue1")
                .build();
    }
    @Bean
    public Binding midengQue1Binding(Queue midengQue1,Exchange normalEx){
        return BindingBuilder
                .bind(midengQue1)
                .to(normalEx)
                .with("mideng.#")
                .noargs();
    }
}
